package com.ruoyi.web.controller.course;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.siyun.domain.SyCourse;
import com.ruoyi.siyun.domain.SyCourseVideo;
import com.ruoyi.siyun.service.ISyCourseService;
import com.ruoyi.siyun.service.ISyCourseVideoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 课程Controller
 * 
 * @author theguo
 * @date 2024-12-19
 */
@RestController
@RequestMapping("/siyun/course")
public class SyCourseController extends BaseController
{
    @Autowired
    private ISyCourseService syCourseService;

    @Autowired
    private ISyCourseVideoService syCourseVideoService;

    /**
     * 查询课程列表
     */
    @PreAuthorize("@ss.hasPermi('siyun:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(SyCourse syCourse)
    {
        startPage();
        List<SyCourse> list = syCourseService.selectSyCourseList(syCourse);
        return getDataTable(list);
    }

    /**
     * 导出课程列表
     */
    @PreAuthorize("@ss.hasPermi('siyun:course:export')")
    @Log(title = "课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SyCourse syCourse)
    {
        List<SyCourse> list = syCourseService.selectSyCourseList(syCourse);
        ExcelUtil<SyCourse> util = new ExcelUtil<SyCourse>(SyCourse.class);
        util.exportExcel(response, list, "课程数据");
    }

    /**
     * 获取课程详细信息
     */
    @PreAuthorize("@ss.hasPermi('siyun:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(syCourseService.selectSyCourseById(id));
    }

    /**
     * 新增课程
     */
    @PreAuthorize("@ss.hasPermi('siyun:course:add')")
    @Log(title = "课程", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SyCourse syCourse) {
        int i = syCourseService.insertSyCourse(syCourse);
        if (i == 0){
             return error("添加错误：请联系管理员");
        }
        List<SyCourseVideo> syCourseVideos = syCourse.getSyCourseVideos();
        for (SyCourseVideo syCourseVideo : syCourseVideos){
            syCourseVideo.setVideoCourseid(syCourse.getId());
            syCourseVideo.setVideoUploaduser(syCourse.getUsername());
            syCourseVideo.setVideoUploadtime(DateUtils.getNowDate());
            int i1 = syCourseVideoService.insertSyCourseVideo(syCourseVideo);
            if (i1 == 0){
                return error("添加错误：请联系管理员");
            }
        }

        return toAjax(i);
    }

    /**
     * 修改课程
     */
    @PreAuthorize("@ss.hasPermi('siyun:course:edit')")
    @Log(title = "课程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SyCourse syCourse) {
        int i = syCourseService.updateSyCourse(syCourse);
        if (i == 0){
            return error("修改错误：请联系技术员");
        }
        List<SyCourseVideo> syCourseVideos = syCourse.getSyCourseVideos();
        if (syCourseVideos !=null){
            for (SyCourseVideo syCourseVideo : syCourseVideos){
                if (syCourseVideo.getVideoCourseid() == 0L){
                    syCourseVideo.setVideoCourseid(syCourse.getId());
                    syCourseVideo.setVideoUploadtime(DateUtils.getNowDate());
                    int i1 = syCourseVideoService.insertSyCourseVideo(syCourseVideo);
                    if (i1 == 0){
                        return error("修改错误：请联系技术员");
                    }
                }else if(syCourseVideo.getVideoCourseid() != 0L){
                    int i1 = syCourseVideoService.updateSyCourseVideo(syCourseVideo);
                    if (i1 == 0){
                        return error("修改错误：请联系技术员");
                    }
                }
            }
        }
        return success("修改成功");
    }
    /**
     * 删除课程
     */
    @PreAuthorize("@ss.hasPermi('siyun:course:remove')")
    @Log(title = "课程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        return toAjax(syCourseService.deleteSyCourseByIds(ids));
    }
}
